﻿Imports System.IO
Imports System.Text
Imports System.Diagnostics
Imports System.Security.Cryptography

Namespace Hash

    Public Class SHA

        Public Enum Tipo As Integer
            Normal = 1
            Criptografia256 = 2
            Criptografia384 = 3
            Criptografia512 = 4
        End Enum

        Public Function Checksum(ByVal Arquivo As String, ByVal Nivel As Tipo) As String
            If Nivel = Tipo.Normal Then
                Dim SHA1 As SHA1CryptoServiceProvider = New SHA1CryptoServiceProvider
                Dim fs As FileStream = New FileStream(Arquivo, FileMode.Open, _
            FileAccess.Read, FileShare.Read, 8192)
                fs = New FileStream(Arquivo, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
                SHA1.ComputeHash(fs)
                fs.Close()
                Dim hash As Byte() = SHA1.Hash
                Dim sb As StringBuilder = New StringBuilder
                Dim hByte As Byte
                For Each hByte In hash
                    sb.Append(String.Format("{0:X2}", hByte))
                Next
                Return sb.ToString()

            ElseIf Nivel = Tipo.Criptografia256 Then
                Dim SHA1 As SHA256Managed = New SHA256Managed
                Dim fs As FileStream = New FileStream(Arquivo, FileMode.Open, _
            FileAccess.Read, FileShare.Read, 8192)
                fs = New FileStream(Arquivo, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
                SHA1.ComputeHash(fs)
                fs.Close()
                Dim hash As Byte() = SHA1.Hash
                Dim sb As StringBuilder = New StringBuilder
                Dim hByte As Byte
                For Each hByte In hash
                    sb.Append(String.Format("{0:X2}", hByte))
                Next
                Return sb.ToString()

            ElseIf Nivel = Tipo.Criptografia384 Then
                Dim SHA1 As SHA384Managed = New SHA384Managed
                Dim fs As FileStream = New FileStream(Arquivo, FileMode.Open, _
            FileAccess.Read, FileShare.Read, 8192)
                fs = New FileStream(Arquivo, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
                SHA1.ComputeHash(fs)
                fs.Close()
                Dim hash As Byte() = SHA1.Hash
                Dim sb As StringBuilder = New StringBuilder
                Dim hByte As Byte
                For Each hByte In hash
                    sb.Append(String.Format("{0:X2}", hByte))
                Next
                Return sb.ToString()

            ElseIf Nivel = Tipo.Criptografia512 Then
                Dim SHA1 As SHA512Managed = New SHA512Managed
                Dim fs As FileStream = New FileStream(Arquivo, FileMode.Open, _
            FileAccess.Read, FileShare.Read, 8192)
                fs = New FileStream(Arquivo, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
                SHA1.ComputeHash(fs)
                fs.Close()
                Dim hash As Byte() = SHA1.Hash
                Dim sb As StringBuilder = New StringBuilder
                Dim hByte As Byte
                For Each hByte In hash
                    sb.Append(String.Format("{0:X2}", hByte))
                Next
                Return sb.ToString()

            End If
        End Function

        Public Function GeraString(ByVal Texto As String, ByVal Nivel As Tipo, Optional ByVal Hexa As Boolean = True) As String
            If Nivel = Tipo.Normal Then
                Dim btyScr() As Byte = ASCIIEncoding.ASCII.GetBytes(Texto)
                Dim objSha1 As New SHA1Managed
                Dim btyRes() As Byte = objSha1.ComputeHash(btyScr)

                If Not Hexa Then
                    Return Convert.ToBase64String(btyRes)
                Else
                    Dim intTotal As Integer = (CInt(btyRes.Length * 2) + CInt((btyRes.Length / 8)))
                    Dim strRes As StringBuilder = New StringBuilder(intTotal)
                    Dim intI As Integer

                    For intI = 0 To btyRes.Length - 1
                        strRes.Append(BitConverter.ToString(btyRes, intI, 1))
                    Next intI

                    Return strRes.ToString().TrimEnd(New Char() {" "c}).ToLower
                End If

            ElseIf Nivel = Tipo.Criptografia256 Then
                Dim btyScr() As Byte = ASCIIEncoding.ASCII.GetBytes(Texto)
                Dim objSha1 As New SHA256Managed
                Dim btyRes() As Byte = objSha1.ComputeHash(btyScr)

                If Not Hexa Then
                    Return Convert.ToBase64String(btyRes)
                Else
                    Dim intTotal As Integer = (CInt(btyRes.Length * 2) + CInt((btyRes.Length / 8)))
                    Dim strRes As StringBuilder = New StringBuilder(intTotal)
                    Dim intI As Integer

                    For intI = 0 To btyRes.Length - 1
                        strRes.Append(BitConverter.ToString(btyRes, intI, 1))
                    Next intI

                    Return strRes.ToString().TrimEnd(New Char() {" "c}).ToLower
                End If

            ElseIf Nivel = Tipo.Criptografia384 Then
                Dim btyScr() As Byte = ASCIIEncoding.ASCII.GetBytes(Texto)
                Dim objSha1 As New SHA384Managed
                Dim btyRes() As Byte = objSha1.ComputeHash(btyScr)

                If Not Hexa Then
                    Return Convert.ToBase64String(btyRes)
                Else
                    Dim intTotal As Integer = (CInt(btyRes.Length * 2) + CInt((btyRes.Length / 8)))
                    Dim strRes As StringBuilder = New StringBuilder(intTotal)
                    Dim intI As Integer

                    For intI = 0 To btyRes.Length - 1
                        strRes.Append(BitConverter.ToString(btyRes, intI, 1))
                    Next intI

                    Return strRes.ToString().TrimEnd(New Char() {" "c}).ToLower
                End If

            ElseIf Nivel = Tipo.Criptografia512 Then
                Dim btyScr() As Byte = ASCIIEncoding.ASCII.GetBytes(Texto)
                Dim objSha1 As New SHA512Managed
                Dim btyRes() As Byte = objSha1.ComputeHash(btyScr)

                If Not Hexa Then
                    Return Convert.ToBase64String(btyRes)
                Else
                    Dim intTotal As Integer = (CInt(btyRes.Length * 2) + CInt((btyRes.Length / 8)))
                    Dim strRes As StringBuilder = New StringBuilder(intTotal)
                    Dim intI As Integer

                    For intI = 0 To btyRes.Length - 1
                        strRes.Append(BitConverter.ToString(btyRes, intI, 1))
                    Next intI

                    Return strRes.ToString().TrimEnd(New Char() {" "c}).ToLower
                End If

            End If
        End Function

    End Class

End Namespace